package com.link2room.optimus.controller.api;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.optimus.dto.param.api.MarketingApiParamDto;
import com.link2room.common.util.DateUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.optimus.dto.result.api.MarketingApiDto;
import com.link2room.optimus.facade.IMarketingApiFacadeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by sss on 2016/9/6.
 */
@Api(tags = "酒店活动")
@RestController
@RequestMapping("/api/Marketings")
public class MarketingApiController {
    @Resource
    private IMarketingApiFacadeService marketingApiFacadeService;

    @RequestMapping(method = RequestMethod.GET)
    public BaseResultDto marketings(
            @RequestParam(value = "lhotelCode" ,required = true) String lhotelCode,
            @RequestParam(value = "checkInDatetime" ,required = true) String checkInDatetime,
            @RequestParam(value = "checkOutDatetime" ,required = true) String checkOutDatetime,
            @RequestParam(value = "mcCodes" ,required = false) String mcCodes
    ) throws Exception {

        MarketingApiParamDto marketingApiParamDtom=new MarketingApiParamDto();
        marketingApiParamDtom.setLhotelCode(lhotelCode);
        Date defaultDate=DateUtil.parseDateNoTime(new Date());
        marketingApiParamDtom.setCheckInDatetime(DateUtil.format(DateUtil.parseDateWevFormat(checkInDatetime),DateUtil.newFormat));
        marketingApiParamDtom.setCheckOutDatetime(DateUtil.format(DateUtil.parseDateWevFormat(checkOutDatetime),DateUtil.newFormat));
        if (StringUtil.isBlank(checkInDatetime)) {
            marketingApiParamDtom.setCheckInDate(defaultDate);
            marketingApiParamDtom.setCheckOutDate(DateUtil.addDays(defaultDate,1L));
        }
        if (StringUtil.isBlank(checkOutDatetime)) {
            marketingApiParamDtom.setCheckOutDate(DateUtil.addDays(marketingApiParamDtom.getCheckInDate(),1L));
        }
        if(StringUtil.isNotBlank(mcCodes)){
            String[] mcCodeArr = mcCodes.split(",");
            List<String> mcCodeList=new ArrayList<>();
            Collections.addAll(mcCodeList,mcCodeArr);
            marketingApiParamDtom.setMcCodeList(mcCodeList);
        }
        MarketingApiDto marketingApiDto = marketingApiFacadeService.getHotelMarketingList(marketingApiParamDtom);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(marketingApiDto);
        return l2rRs;
    }
}
